<?php
namespace Mobile\Controller;
use Think\Controller;
class UserController extends Controller 
{
	//手机登陆
    public function login()
    {
        if (IS_POST) {
        $data['phone'] = I('post.phone');
        $data['password'] = I('post.password');

        //记住用户名密码
        setcookie('shoujname',$data['phone'],time()+1209600);  
        setcookie('password',$data['password'],time()+1209600);

         //手机
         $preg_js = "/^1[34578][0-9]{9}$/";
         $pass_sj = preg_match($preg_js, $data['phone']);
         if (!$pass_sj) {
            $arr = array(
                'error' => 0,
                'info' => '请输入正确的手机号'
                );
            echo json_encode($arr);
            exit;
         }

         $jiazhang_user = M('jiazhang_user')->where(array('phone'=>$data['phone'],'password'=>md5($data['password'])))->find();
         $laoshi_user = M('laoshi_user')->where(array('phone'=>$data['phone'],'password'=>md5($data['password'])))->find();
         //判断是老师还是家长的手机
         if ($jiazhang_user) {

             //判断冻结，删除账号
             if ($jiazhang_user['is_dongjie']==0 ) {
                $arr = array(
                    'error' => 0,
                    'info' => '账号已冻结'
                    );
                echo json_encode($arr);
                exit;
             }
             if ($jiazhang_user['is_del']==1 ) {
                $arr = array(
                    'error' => 0,
                    'info' => '账号已删除'
                    );
                echo json_encode($arr);
                exit;
             }

            $url = U('mobile/info/jz_home');
            $arr = array(
                'error' => 1,
                'info' => '欢迎家长',
                'url' => $url
                );
            echo json_encode($arr);
            
            //更新登陆时间
            $login_time = time();
            M('jiazhang_user')->where(array('phone'=>$data['phone']))->setField('login_time',$login_time);
            //登陆成功 写session
            $_SESSION["id"] = $jiazhang_user["id"];
            $_SESSION["phone"] = $jiazhang_user["phone"];
            $_SESSION["is_who"] = 'jiazhang';

         }elseif ($laoshi_user) {

             //判断冻结，删除账号
             if ($laoshi_user['is_dongjie']==0 ) {
                $arr = array(
                    'error' => 0,
                    'info' => '账号已冻结'
                    );
                echo json_encode($arr);
                exit;
             }
             if ($laoshi_user['is_del']==1 ) {
                $arr = array(
                    'error' => 0,
                    'info' => '账号已删除'
                    );
                echo json_encode($arr);
                exit;
             }
            $url = U('mobile/info/ls_home');
            $arr = array(
                'error' => 1,
                'info' => '欢迎老师',
                'url' => $url
                );
            echo json_encode($arr);

            //更新登陆时间
            $login_time = time();
            M('laoshi_user')->where(array('phone'=>$data['phone']))->setField('login_time',$login_time);
            //登陆成功 写session
            $_SESSION["id"] = $laoshi_user["id"];
            $_SESSION["phone"] = $laoshi_user["phone"];
            $_SESSION["is_who"] = 'laoshi';

         }else {
            $arr = array(
                'error' => 0,
                'info' => '账号密码错误,请确认后在输入'
                );
            echo json_encode($arr);
         }
            
        }else {
            //首页显示城市
            $city = I('get.newcity')?I('get.newcity'):'广州';
            $this->assign('city',$city);

         $this->display();   
        }
    }

    //选择 学员/老师 登陆界面
    public function zhuce()
    {
      //首页显示城市
      $city = I('get.newcity')?I('get.newcity'):'广州';
      $this->assign('city',$city);
      
      $this->display();
    }

    //验证码
    public function vcode()
    {
        ob_clean();
        $Verify = new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length = 4;
        //$Verify->useNoise = false;
        $Verify->entry();
    }

    //老师注册
    public function ls_zhuce()
    {  
       if (IS_POST) {
         $data['phone'] = I('post.phone');

         // $vcode = I('post.vcode');
         $send_to = I('post.send_to');

         $data['password'] = I('post.password');
         $repassword = I('post.repassword');
         $data['city'] = I('post.city');

         //手机
         $preg_js = "/^1[34578][0-9]{9}$/";
         $pass_sj = preg_match($preg_js, $data['phone']);
         if (!$pass_sj) {
            $arr = array(
                'error' => 0,
                'info' => '请输入正确的手机号'
                );
            echo json_encode($arr);
            exit;
         }

         //存在数据库验证
         $sql = 'select * from laoshi_user where phone='.$data['phone'];
         $res_phone = M()->query($sql);
         if ($res_phone) {
            $arr = array(
                'error' => 0,
                'info' => '手机号已存在,请重新输入'
                );
            echo json_encode($arr);
            exit;
         }
         //存在数据库唯一验证
         $sql = 'select * from jiazhang_user where phone='.$data['phone'];
         $jiazhang_phone = M()->query($sql);
         if ($jiazhang_phone) {
            $arr = array(
                'error' => 0,
                'info' => '手机号已注册家长账号,请更换'
                );
            echo json_encode($arr);
            exit;
         }

         //验证码
        // $verify = new \Think\Verify(); 
        // if (!$verify->check($vcode)) {
        //     $arr = array(
        //         'error' => 0,
        //         'info' => '验证码错误'
        //         );
        //     echo json_encode($arr);
        //     exit;
        // }

        //短信随机码验证
        if ($send_to != $_SESSION['sj_mobile']) {
            $arr = array(
                'error' => 0,
                'info' => '短信码错误'
                );
            echo json_encode($arr);
            exit;
        }

        //密码
        $preg_pass = "/^[0-9A-z]{6,16}$/";
        $pass_Yz = preg_match($preg_pass, $data['password']);
        if (!$pass_Yz) {
            $arr = array(
                'error' => 0,
                'info' => '密码必须是6-16位的字符串'
                );
            echo json_encode($arr);
            exit;
        }
        //两次密码一致验证
        if ($data['password'] != $repassword) {
            $arr = array(
                'error' => 0,
                'info' => '第二次密码输入有误'
                );
            echo json_encode($arr);
            exit;
        }

        $data['password'] = md5($data['password']);
        $data['create_time'] = time();
        $data['create_ip'] =  get_client_ip();

        //入库
        $res = M('laoshi_user')->add($data);
           if ($res) {
                 $arr = array(
                    'error' =>1,
                    'info'=>"注册成功"
                    );
                  echo json_encode($arr);
             }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"注册失败"
                        );
                echo json_encode($arr);
             }

       }else {

        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

        //城市显示
        $citys = M('city')->where('parent_id=0')->select();
        $this->assign('citys',$citys);
        
        $this->display();
       }
    }
    //家长注册
    public function jz_zhuce()
    {   //Array ( [phone] => 11 [send_to] => 1 [password] => 11 [repassword] => 11 [city] => 广州 )
        if (IS_POST) {
             $data['phone'] = I('post.phone');

             // $vcode = I('post.vcode');
             $send_to = I('post.send_to');

             $data['password'] = I('post.password');
             $repassword = I('post.repassword');
             $data['city'] = I('post.city');

             //手机
             $preg_js = "/^1[34578][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
             //存在数据库验证
             $sql = 'select * from jiazhang_user where phone='.$data['phone'];
             $res_phone = M()->query($sql);
             if ($res_phone) {
                $arr = array(
                    'error' => 0,
                    'info' => '手机号已存在,请重新输入'
                    );
                echo json_encode($arr);
                exit;
             }
             //存在数据库唯一验证
             $sql = 'select * from laoshi_user where phone='.$data['phone'];
             $laoshi_phone = M()->query($sql);
             if ($laoshi_phone) {
                $arr = array(
                    'error' => 0,
                    'info' => '手机号已注册为老师账号,请更换'
                    );
                echo json_encode($arr);
                exit;
             }

             //验证码
            // $verify = new \Think\Verify(); 
            // if (!$verify->check($vcode)) {
            //     $arr = array(
            //         'error' => 0,
            //         'info' => '验证码错误'
            //         );
            //     echo json_encode($arr);
            //     exit;
            // }

            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '短信码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //密码
            $preg_pass = "/^[0-9A-z]{6,16}$/";
            $pass_Yz = preg_match($preg_pass, $data['password']);
            if (!$pass_Yz) {
                $arr = array(
                    'error' => 0,
                    'info' => '密码必须是6-16位的字符串'
                    );
                echo json_encode($arr);
                exit;
            }
            //两次密码一致验证
            if ($data['password'] != $repassword) {
                $arr = array(
                    'error' => 0,
                    'info' => '第二次密码输入有误'
                    );
                echo json_encode($arr);
                exit;
            }

            $data['password'] = md5($data['password']);
            $data['create_time'] = time();
            $data['create_ip'] =  get_client_ip();

            //入库
            $res = M('jiazhang_user')->add($data);
               if ($res) {
                     $arr = array(
                        'error' =>1,
                        'info'=>"注册成功"
                        );
                      echo json_encode($arr);
                 }else {
                     $arr = array(
                            'error' =>0,
                            'info'=>"注册失败"
                            );
                    echo json_encode($arr);
                 }
            
        }else {
            //首页显示城市
            $city = I('get.newcity')?I('get.newcity'):'广州';
            $this->assign('city',$city);

            //城市显示
            $citys = M('city')->where('parent_id=0')->select();
            $this->assign('citys',$citys);

            $this->display(); 
        }
    }

    //退出登陆
    public function logout()
    {
        session_unset();
        session_destroy();
        $this->redirect('mobile/index/index');
    }

    //重置密码 显示
    public function repassword()
    {
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

        $this->display();
    }
    //重置密码 
    public function repassword_re()
    {//Array ( [sf] => 老师 [phone] => 13430372763 [send_to] => 11 [password] => 11 )
        if (IS_POST) {
            $sf = I('post.sf');
            $phone = I('post.phone');
            $send_to = I('post.send_to');
            $password = I('post.password');

             //手机
             $preg_js = "/^1[34578][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $phone);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '短信码错误'
                    );
                echo json_encode($arr);
                exit;
            }
            //密码
            $preg_pass = "/^[0-9A-z]{6,16}$/";
            $pass_Yz = preg_match($preg_pass, $password);
            if (!$pass_Yz) {
                $arr = array(
                    'error' => 0,
                    'info' => '密码必须是6-16位的字符串'
                    );
                echo json_encode($arr);
                exit;
            }
            //是老师 或 家长
            if ($sf == '老师') {
                $res = M('laoshi_user')->where(array('phone'=>$phone))->setField('password',md5($password));
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '重置成功'
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '重置失败'
                        );
                    echo json_encode($arr);
                }
            }elseif ($sf == '会员') {
                $res = M('jiazhang_user')->where(array('phone'=>$phone))->setField('password',md5($password));
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '重置成功'
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '重置失败'
                        );
                    echo json_encode($arr);
                }
            }
        }
    }


    //尚门协议 老师协议显示
    public function ls_article()
    {
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

        $stutags = M('stutags')->where('id=9')->getField('content');
        $this->assign('stutags',$stutags);

        $this->display();
    }
    //尚门协议 家长协议显示
    public function jz_article()
    {
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

        $stutags = M('stutags')->where('id=10')->getField('content');
        $this->assign('stutags',$stutags);

        $this->display();
    }


    //秒滴短信发送开始----------------------
    public function send_to()
    {
        if (IS_POST) {
            $to = I('post.to');

             //手机
             $preg_js = "/^1[34578][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $to);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

             //发送
            miaodi($to);
            $arr = array(
                'error' => 1,
                'info' => '正在发送...',
                );
            echo json_encode($arr);
             
        }
    }









}